package codetop.greedy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Code621 {
    public int leastInterval(char[] tasks, int n) {
        List<Character> list=new ArrayList<>();
        for (int i = 0; i < tasks.length; i++) {
            list.add(tasks[i]);
        }
        Map<Character,Integer>map=new HashMap<>();
        int time=0;
        while (!list.isEmpty()){
            int i;
            for (i = 0; i < list.size(); i++) {
                char c=list.get(i);
                if (!map.containsKey(c)||(time-map.get(c))>n){
                    map.put(c,time);
                    break;
                }
            }
            if (i<list.size())list.remove(i);
            time++;
        }
        return time;
    }
}
